<?php
/**
 * Template for day views.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once 'login_info.php';
require_once 'schedule.php';

loginInfo::update_login ();

$day_str = $_GET['day'];

// load day variables.

$file = sprintf ("%s/%s.php", schedule::sched_dir(), $day_str);

include $file;

$year = 0;
$month = 0;
$d = 0;
list($year, $month, $d) = sscanf ($day_str, "%d_%d_%d");

$month_name = date("F", mktime(0,0,0,(int)$month,1,(int)$year));

$next_date = getdate(mktime(0,0,0,(int)$month,(int)$d+1,(int)$year));
$prev_date = getdate(mktime(0,0,0,(int)$month,(int)$d-1,(int)$year));

$next_file = sprintf("%d_%d_%d", (int)$next_date['year'], (int)$next_date['mon'], (int)$next_date['mday']);
$prev_file = sprintf("%d_%d_%d", (int)$prev_date['year'], (int)$prev_date['mon'], (int)$prev_date['mday']);

// will set $day_vars 

$machs = array();
$db = new database ();

$result = $db->query("SELECT id,name FROM Machines");
while ($row = $result->fetch_assoc ()) {
	$machs[$row['id']] = $row['name'];
}
$result->free ();
$db->close ();

$span = count($machs) - 1;

?>

<html>
	<head>
		<title>Opol - Schedule For <?php print "$day_str";?> </title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<table border="1" class='sched'>
			<tr>
				<td class='sched_nav'><?php print "<a href='/view_day.php?day=$prev_file'>";?>&lt;</a></td>
				<td class='sched_name' colspan="<?php print $span; ?>"><?php print "Schedule for $month_name $d, $year";?></td>
				<td class='sched_nav'><?php print "<a href='/view_day.php?day=$next_file'>";?>&gt;</td>
			</tr>
			<tr>
					<td></td>
<?php
	foreach ($machs as $mid => $name) {
		print "\t\t\t\t<td><b>$name</b></td>\n";
	}
?>
			</tr>
<?php
if (isset ($GLOBALS['day_vars']))
	$day_vars = $GLOBALS['day_vars'];
foreach (range(0,23) as $hour) {
	print "\t\t\t<tr>\n";
	if (isset ($day_vars) && isset ($day_vars[$hour]))
		$hour_vars = $day_vars[$hour];
	$hour_out = $hour % 12 == 0 ? 12 : $hour % 12;
	$hour_str = sprintf ("%d:00 %s", $hour_out, $hour > 11 ? "pm" : "am"); 
	printf ("\t\t\t\t<td>%s</td>\n", $hour_str);
	foreach ($machs as $mid => $name) {
		print "\t\t\t\t<td>\n";
		if (isset ($hour_vars) && isset ($hour_vars[$mid])) {
			$mach_vars = $hour_vars[$mid];
			print "\t\t\t\t\t<ul class='phase_list'>\n";
			foreach ($mach_vars as $string) {
				print "\t\t\t\t\t\t<li>$string</li>\n";
			}
			print "\t\t\t\t\t</ul>\n";
		}
		print "\t\t\t\t</td>\n";
	}
	print "\t\t\t</tr>\n";
}
?>
		</table>
	</body>
</html>
